package com.xg.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * 向业务前置发送请求
 *
 * @author dehuisun
 */
@Slf4j
public class TcpUtils {
    /**
     * 读取超时时间
     */
    private static int timeOut = 200;

    /**
     * tcpPost
     *
     * @throws IOException
     */
    public static String post(String ip, int port, String data) throws IOException {
        Socket socket = null;
        InputStream is = null;
        byte message[] = null;
        DataOutputStream dos = null;
        String value = "";
        try {
            String tradeMessage = data;
            socket = new Socket(ip, port);
            socket.setSoTimeout(timeOut);
            byte[] msg = tradeMessage.getBytes("UTF-8");
            dos = new DataOutputStream(socket.getOutputStream());
            dos.write(msg);
            dos.flush();
            message = new byte[1024];
            is = socket.getInputStream();
            is.read(message);//接收服务器的响应信息
            value = new String(message);
            System.out.println("返回的数据：" + value);
        } catch (Exception e) {
            log.error("tcp连接异常",e);
        } finally {// 关闭连接和I/O,释放资源
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(dos);
            if (socket != null) {
                socket.close();
            }
        }
        return value;
    }

    /**
     * tcpPost
     *
     * @throws IOException
     */
    public static void postwithoutresponse(String ip, int port, String data) throws IOException {
        Socket socket = null;
        DataOutputStream dos = null;
        try {
            String tradeMessage = data;
            socket = new Socket(ip, port);
            socket.setSoTimeout(timeOut);
            byte[] msg = tradeMessage.getBytes("UTF-8");
            dos = new DataOutputStream(socket.getOutputStream());
            dos.write(msg);
            dos.flush();

        } catch (Exception e) {
            log.error("tcp连接异常",e);
            throw e;
        } finally {// 关闭连接和I/O,释放资源
            IOUtils.closeQuietly(dos);
            if (socket != null) {
                socket.close();
            }
        }
    }

    /**
     * tcpPost返回字节流
     *
     * @throws IOException
     */
    public static byte[] postReByte(String ip, int port, String data) throws IOException {
        Socket socket = null;
        InputStream is = null;
        byte message[] = null;
        DataOutputStream dos = null;
        try {
            String tradeMessage = data;
            socket = new Socket(ip, port);
            socket.setSoTimeout(timeOut);
            byte[] msg = tradeMessage.getBytes("UTF-8");
            dos = new DataOutputStream(socket.getOutputStream());
            dos.write(msg);
            dos.flush();
            message = new byte[1024];
            is = socket.getInputStream();
            is.read(message);//接收服务器的响应信息
            //			System.out.println("返回的数据：");
            //			value = new String(message);
        } catch (Exception e) {
            log.error("tcp连接异常",e);
        } finally {// 关闭连接和I/O,释放资源
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(dos);
            if (socket != null) {
                socket.close();
            }
        }
        return message;
    }

    /**
     * tcpPost   按照发送字节
     *
     * @throws IOException
     */
    @SuppressWarnings("finally")
    public static String postBytes(String ip, int port, byte[] msg) throws Exception {
        Socket socket = null;
        InputStream is = null;
        byte message[] = null;
        DataOutputStream dos = null;
        String value = "";
        try {
            socket = new Socket(ip, port);
            socket.setSoTimeout(timeOut);
            dos = new DataOutputStream(socket.getOutputStream());
            dos.write(msg);
            dos.flush();
            message = new byte[1024];
            is = socket.getInputStream();
            is.read(message);//接收服务器的响应信息
            value = new String(message);
        } catch (Exception e) {
            log.error("tcp连接异常",e);
        } finally {
            // 关闭连接和I/O,释放资源
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(dos);
            if (socket != null) {
                socket.close();
            }
            return value;
        }

    }


    /**
     * tcpPost返回指定字节的16进制字符串
     *
     * @throws IOException
     */
    public static String postRe16Self(String ip, int port, byte[] data, int length) throws IOException {
        Socket socket = null;
        InputStream is = null;
        byte message[] = null;
        DataOutputStream dos = null;
        String mes = "";
        try {
            socket = new Socket(ip, port);
            socket.setSoTimeout(timeOut);
            dos = new DataOutputStream(socket.getOutputStream());
            dos.write(data);
            dos.flush();
            message = new byte[length];
            is = socket.getInputStream();
            is.read(message);//接收服务器的响应信息
            mes = HexStrConver.byte2HexStr(message);
            System.out.println("返回的数据：" + mes);
            //			value = new String(message);
        } catch (Exception e) {
            log.error("tcp连接异常",e);
        } finally {// 关闭连接和I/O,释放资源
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(dos);
            if (socket != null) {
                socket.close();
            }
        }
        return mes;
    }


    /**
     * tcpPost返回16进制字符串
     *
     * @throws IOException
     */
    public static String postRe16(String ip, int port, String data) throws IOException {
        Socket socket = null;
        InputStream is = null;
        byte message[] = null;
        DataOutputStream dos = null;
        String mes = "";
        try {
            String tradeMessage = data;
            socket = new Socket(ip, port);
            socket.setSoTimeout(timeOut);
            byte[] msg = tradeMessage.getBytes("UTF-8");
            dos = new DataOutputStream(socket.getOutputStream());
            dos.write(msg);
            dos.flush();
            message = new byte[1024];
            is = socket.getInputStream();
            is.read(message);//接收服务器的响应信息
            mes = HexStrConver.byte2HexStr(message);
            System.out.println("返回的数据：" + mes);
            //			value = new String(message);
        } catch (Exception e) {
            log.error("tcp连接异常",e);
        } finally {// 关闭连接和I/O,释放资源
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(dos);
            if (socket != null) {
                socket.close();
            }
        }
        return mes;
    }


    /**
     * 特殊处理为监控PLC状态
     * tcpPost16进制字符串
     *
     * @throws IOException
     */
    public static String post16Re16(String ip, int port, String data) throws IOException {
        Socket socket = null;
        InputStream is = null;
        byte message[] = null;
        DataOutputStream dos = null;
        String mes = "";
        try {
            socket = new Socket(ip, port);
            socket.setSoTimeout(timeOut);
            byte[] msg = HexStrConver.hexStringToBytes(data);
            //	byte[] msg = tradeMessage.getBytes("UTF-8");
            dos = new DataOutputStream(socket.getOutputStream());
            dos.write(msg);
            dos.flush();
            message = new byte[1];
            //			message=HexStrConver.toByteArray(data);
            is = socket.getInputStream();
            is.read(message);//接收服务器的响应信息
            mes = HexStrConver.byte2HexStr(message);
            System.out.println("返回的数据：" + mes);
            //			value = new String(message);
        } catch (Exception e) {
            log.error("tcp连接异常",e);
        } finally {// 关闭连接和I/O,释放资源
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(dos);
            if (socket != null) {
                socket.close();
            }
        }
        return mes;
    }


    /**
     * tcpPost 测试请求
     *
     * @throws IOException
     */
    public static boolean postBytesReBoolen(String ip, int port, byte[] msg) throws Exception {
        Socket socket = null;
        DataOutputStream dos = null;
        try {
            socket = new Socket();
            socket.connect(new InetSocketAddress(ip, port), 100);//设置连接请求超时时间100 毫秒
            socket.setSoTimeout(100);
            dos = new DataOutputStream(socket.getOutputStream());
            dos.write(msg);
            dos.flush();
            return true;
        } catch (Exception e) {
            return false;
        } finally {// 关闭连接和I/O,释放资源
            IOUtils.closeQuietly(dos);
            if (socket != null) {
                socket.close();
            }
        }
    }
}
